package com.graduation_design.modules.app.controller;

import cn.hutool.core.util.StrUtil;
import com.graduation_design.common.lang.Result;
import com.graduation_design.entity.AppComment;
import com.graduation_design.modules.app.annotation.Login;
import com.graduation_design.service.AppCommentService;
import com.graduation_design.service.AppUploadService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/app/comment")
public class CommentController extends BaseController {

    @Resource
    AppCommentService appCommentService;

    @Resource
    AppUploadService appUploadService;

    /**
     * 商品评价列表
     */
    @GetMapping("/product")
    public Result product(Long productId) {
        return Result.success(appCommentService.pageByProductId(getPage(), productId));
    }

    /**
     * 待评价
     * @param status 0未评论，1已评论
     */
    @Login
    @GetMapping("/list")
    public Result list(Integer status) {
        return Result.success(appCommentService.getPage(getPage(), status));
    }

    /**
     * 评价信息
     */
    @Login
    @GetMapping("/info")
    public Result info(Long id) {
        return Result.success(appCommentService.getById(id));
    }

    /**
     * 发表评价
     */
    @Login
    @PostMapping("/post")
    public Result post(MultipartFile[] pics, @Validated AppComment comment) throws IOException {

        if (pics != null && pics.length > 0) {
            List<String> paths = appUploadService.upload(pics, 0.5F, 0.3);
            comment.setImages(StrUtil.join(";", paths));
        }

        long id = appCommentService.post(comment);
        return Result.success(id);
    }
}
